package jlb.andriva.wrappers;

import android.app.Activity;
import android.content.Context;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import jlb.andriva.R;

public class Settings {

    private Activity mActivity;
    public String user = "";

    public Settings(Activity activity) {
        mActivity = activity;
    }

    public void loadSettings() {
        InputStream instream = null;
        List<String> mdata = new ArrayList<String>();
        try {
            instream = new FileInputStream(mActivity.getFilesDir() + "/" + mActivity.getString(R.string.file_name));
            InputStreamReader inputreader = new InputStreamReader(instream);
            BufferedReader buffreader = new BufferedReader(inputreader);
            String line;
            do {
                line = buffreader.readLine();
                if(line != null) mdata.add(line);
            } while (line != null);
            if(mdata.size() > 0){
                user = mdata.get(0);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            try {
                instream.close();
            } catch (Exception ex) {}
        }
    }

    public void saveSettings() {
        String str = "";
        str += user;
        FileOutputStream outputStream = null;
        try {
            outputStream = mActivity.openFileOutput(mActivity.getString(R.string.file_name), Context.MODE_PRIVATE);
            outputStream.write(str.getBytes());
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
        finally{
            try {
                outputStream.close();
            } catch (Exception ex) {}
        }
    }
}